#!/bin/bash
#
# Copyright (C) 2014 EfficiOS Inc.
#
# SPDX-License-Identifier: LGPL-2.1-only

TEST_DESC="Machine interface testing"

CURDIR=$(dirname $0)/
TESTDIR=$CURDIR/../../../

#Test app for ust event
TESTAPP_PATH="$TESTDIR/utils/testapp"
TESTAPP_NAME="gen-ust-events"
TESTAPP_BIN="$TESTAPP_PATH/$TESTAPP_NAME/$TESTAPP_NAME"
NR_USEC_WAIT=0
NR_ITER=10000

#Temp file output
OUTPUT_DIR=$(mktemp -d -t tmp.test_mi_output_dir.XXXXXX)
SESSIOND_LOAD_DIR=$(mktemp -d -t tmp.test_mi_sessiond_load_dir.XXXXXX)
OUTPUT_FILE="default.xml"

XPATH_CMD_OUTPUT="//lttng:command/lttng:output"
XPATH_COMMAND_SUCCESS="/lttng:command/lttng:success/text()"
XPATH_SESSION="$XPATH_CMD_OUTPUT/lttng:sessions/lttng:session"
XPATH_ENABLE_CHANNEL_NAME="$XPATH_CMD_OUTPUT/lttng:channels/lttng:channel/lttng:name/text()"
XPATH_ENABLE_EVENT_SUCCESS="$XPATH_CMD_OUTPUT/lttng:events/lttng:event/lttng:success/text()"
XPATH_DISABLE_EVENT_SUCCESS="$XPATH_CMD_OUTPUT/lttng:channel/lttng:events/lttng:event/lttng:success/text()"
XPATH_LIST_DOMAIN="$XPATH_CMD_OUTPUT/lttng:sessions/lttng:session/lttng:domains/lttng:domain"
XPATH_LIST_CHANNEL="$XPATH_CMD_OUTPUT/lttng:sessions/lttng:session/lttng:domains/lttng:domain/lttng:channels/lttng:channel"
XPATH_LIST_UST_EVENT="$XPATH_CMD_OUTPUT/lttng:domains/lttng:domain[./lttng:type ='UST']/lttng:pids/lttng:pid/lttng:events/lttng:event"
XPATH_SNAPSHOT_ADD_SNAPSHOT="$XPATH_CMD_OUTPUT/lttng:snapshot_action[./lttng:name = 'add-output']/lttng:output"
XPATH_SNAPSHOT_LIST="$XPATH_CMD_OUTPUT/lttng:snapshot_action[./lttng:name = 'list-output']/lttng:output"
XPATH_SNAPSHOT_DEL="$XPATH_CMD_OUTPUT/lttng:snapshot_action[./lttng:name = 'del-output']/lttng:output"
XPATH_TRACK_UNTRACK_VPID="$XPATH_CMD_OUTPUT/lttng:process_attr_trackers/lttng:vpid_process_attr_tracker/lttng:process_attr_values/lttng:vpid/lttng:type"
XPATH_VPID_TRACKER="$XPATH_CMD_OUTPUT/lttng:sessions/lttng:session/lttng:domains/lttng:domain/lttng:process_attr_trackers/lttng:vpid_process_attr_tracker"

DEVNULL=/dev/null 2>&1

DIR=$(readlink -f $TESTDIR)

NUM_TESTS=246

source $TESTDIR/utils/utils.sh

#Overwrite the lttng_bin to get mi output
LTTNG_BIN="lttng --mi xml"

#Global declaration for simplification
LTTNG=$TESTDIR/../src/bin/lttng/$LTTNG_BIN

#Overwrite the default output for utils.sh command

#MUST set TESTDIR before calling those functions
plan_tests $NUM_TESTS

print_test_banner "$TEST_DESC"

function extract_xml ()
{
	local xml=$1
	local xpath=$2
	local __result=$3
	local output
	output=$($XML_EXTRACT $xml "$xpath")
	ok $? "Extraction of xpath $xpath"
	eval $__result="'$output'"
}

function node_check_xml ()
{
	local xml=$1
	local xpath=$2
	local __result=$3
	local output
	output=$($XML_NODE_CHECK $xml "$xpath")
	ok $? "Check of node existence with xpath $xpath"
	eval $__result="'$output'"
}

# Arg1 is path to xml file
# Arg2:
#     is true if we expected the success element to be false
#     else
#     passe false

function is_command_success ()
{
	local xml=$1
	local expect_false=$2

	local xpath=$XPATH_COMMAND_SUCCESS

	#Extract the success element
	#expect false
	extract_xml $OUTPUT_DEST $XPATH_COMMAND_SUCCESS result
	if [[ $expect_false = "true" ]]; then
		if [[ $result ]]; then
			pass "Mi test: $xml command failed as expected"
		else
			fail "Mi test: $xml command did not fail as expected"
		fi
	else
		if [[ $result ]]; then
			pass "Mi test: $xml command success"
		else
			fail "Mi test: $xml command failed"
		fi
	fi
}

function mi_print_version ()
{
	local output_path=$1
	local opt=$2

	$LTTNG version $opt > $output_path
	ok $? "MI test: Lttng version"

}

function test_version ()
{
	OUTPUT_FILE="version.xml"
	OUTPUT_DEST=$OUTPUT_DIR/$OUTPUT_FILE
	mi_print_version $OUTPUT_DEST
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "MI test: Version xsd validation"
}

function test_create_session ()
{
	diag "Test create session"

	local session_name="testSession"

	OUTPUT_FILE="create_session.xml"
	OUTPUT_DEST=$OUTPUT_DIR/$OUTPUT_FILE
	create_lttng_session_ok $session_name $OUTPUT_DIR
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "MI test: create session xsd validation"

	#try to recreate a session. Expecting it to fail
	create_lttng_session_fail $session_name $OUTPUT_DIR
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "MI test: expecting fail create session xsd validation"
	is_command_success $OUTPUT_DEST true

	OUTPUT_DEST=$DEVNULL
	destroy_lttng_session_ok $session_name
}

function test_destroy_session ()
{
	diag "Test destroy session"

	local session_name=(
			"testSession1"
			"testSession2"
			"testSession3")

	OUTPUT_FILE="destroy_session.xml"

	#Test build up
	OUTPUT_DEST=$DEVNULL
	for (( i = 0; i < 3; i++ )); do
		create_lttng_session_ok ${session_name[$i]} $OUTPUT_DIR
	done

	OUTPUT_DEST=$OUTPUT_DIR/$OUTPUT_FILE
	destroy_lttng_session_ok ${session_name[0]}
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "MI test: destroy session ${session_name[0]} xsd validation"

	#Verify that we destroyed the good session
	extract_xml $OUTPUT_DEST $XPATH_SESSION"/lttng:name/text()" result
	test "$result" = "${session_name[0]}"
	ok $? "Mi test: deleted session: $result expected: ${session_name[0]}"

	#Destroy all and count:should be 2
	destroy_lttng_sessions
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "MI test: destroy all session xsd validation"

	#Verify that we destroyed 2 sessions
	extract_xml $OUTPUT_DEST $XPATH_SESSION"/lttng:name/text()" result
	num=$(echo "$result" | wc -l)
	test "$num" -eq "2"
	ok $? "Mi test: $num / 2 sessions discovered"

	#Verify that if no session is present command fail and mi report it.
	destroy_lttng_session_fail ${session_name[0]}
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "MI test: failing destroy session ${session_name[0]} xsd validation"
	extract_xml $OUTPUT_DEST $XPATH_COMMAND_SUCCESS result
	test "$result" = "false"
	ok $? "Mi test: destroy command on ${session_name[0]} failed as expected"

}

function test_list_sessions ()
{
	diag "Test list sessions"

	local session_name=(
			"testSession1"
			"testSession2"
			"testSession3")

	OUTPUT_FILE="list_sessions.xml"

	#Test buid up
	OUTPUT_DEST=$DEVNULL
	for (( i = 0; i < 3; i++ )); do
		create_lttng_session_ok ${session_name[$i]} $OUTPUT_DIR
	done

	OUTPUT_DEST=$OUTPUT_DIR/$OUTPUT_FILE
	list_lttng_ok
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "Mi test: list sessions xsd validation"

	#We should have 3 session
	extract_xml $OUTPUT_DEST $XPATH_SESSION"/lttng:name/text()" result
	num=$(echo "$result" | wc -l)
	test "$num" -eq "3"
	ok $? "Mi test: $num / 3 sessions discovered"

	#Teardown
	OUTPUT_DEST=$DEVNULL
	destroy_lttng_sessions
}

function test_list_session_long_path ()
{
	diag "Test list session long path"

	local session_name="session_long_path"

	output_basedir=$OUTPUT_DIR/$(randstring 254 0)
	mkdir -p ${output_basedir}

	OUTPUT_FILE="list_sessions.xml"

	#Test build up
	OUTPUT_DEST=$DEVNULL
	create_lttng_session_ok $session_name ${output_basedir}

	OUTPUT_DEST=${output_basedir}/$OUTPUT_FILE
	list_lttng_ok
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "Mi test: list session with long path xsd validation"

	#Teardown
	OUTPUT_DEST=$DEVNULL
	destroy_lttng_sessions
}
function test_ust_channel ()
{
	diag "Test UST channel"

	local session_name="testsession"
	local channel_name=("channelUst0" "channelUst1" "channelUst2")

	OUTPUT_FILE="ust_channel.xml"

	#Test buil up
	OUTPUT_DEST=$DEVNULL
	create_lttng_session_ok $session_name $OUTPUT_DIR

	#Test the enable_channel command
	OUTPUT_DEST=$OUTPUT_DIR/$OUTPUT_FILE
	enable_ust_lttng_channel_ok $session_name ${channel_name[0]}
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "Mi test: enable ust channel xsd validation"
	is_command_success $OUTPUT_DEST false

	#Expect the command to fail
	enable_ust_lttng_channel_fail $session_name ${channel_name[0]}
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "Mi test: fail enable ust channel xsd validation"
	is_command_success $OUTPUT_DEST true

	#Create two ust channel to test multiple disable
	for (( i = 1; i < 3; i++ )); do
		enable_ust_lttng_channel_ok $session_name ${channel_name[$i]}
	done

	#Test the disable_channel command
	disable_ust_lttng_channel $session_name ${channel_name[0]}
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "Mi test: disable ust channel xsd validation"
	is_command_success $OUTPUT_DEST false
	#Check that we delete the good channel
	extract_xml $OUTPUT_DEST $XPATH_ENABLE_CHANNEL_NAME result
	test "$result" = "${channel_name[0]}"
	ok $? "MI test: ${channel_name[0]} disabled"

	#Test multiple disable_channel;
	disable_ust_lttng_channel $session_name ${channel_name[1]},${channel_name[2]}
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "Mi test: multiple disable ust channel xsd validation"
	is_command_success $OUTPUT_DEST false

	#Make sure we have two disabled channel
	extract_xml $OUTPUT_DEST $XPATH_ENABLE_CHANNEL_NAME result
	local num=$(echo "$result" | wc -l)
	test "$num" = "2"
	ok $? "Mi test: disabled ust channel $num/2"

	#Teardown
	OUTPUT_DEST=$DEVNULL
	destroy_lttng_sessions

}

function test_ust_lttng_event ()
{
	diag "Test UST event"

	local session_name="testSession"
	local channel_name="testChannel"
	local event=("ev1" "ev2" "ev3")

	OUTPUT_FILE="ust_event.xml"

	#Test build up
	OUTPUT_DEST=$DEVNULL
	create_lttng_session_ok $session_name $OUTPUT_DIR
	enable_ust_lttng_channel_ok $session_name $channel_name

	OUTPUT_DEST=$OUTPUT_DIR/$OUTPUT_FILE
	#Test the enable event for ust domain
	enable_ust_lttng_event_ok $session_name ${event[0]} $channel_name
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "Mi test: Enable_event for ust domain xsd validation"
	is_command_success $OUTPUT_DEST false

	#Try to reenable an enabled event. Should fail
	enable_ust_lttng_event_fail $session_name ${event[0]} $channel_name
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "Mi test: Fail enable_event for ust domain xsd validation"
	is_command_success $OUTPUT_DEST true

	#Enable multiple event including the last one command will fail
	#But 2 events should success

	#Nice little hack, create a string ev,ev,ev
	#IFS stand for Internal field separator
	#Event[*] print all the element inside the array with the IFS in between
	concat_event=$( IFS=$','; echo "${event[*]}" )

	enable_ust_lttng_event_fail $session_name $concat_event $channel_name
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "Mi test: Multiple enable ust event with inside fail xsd validation"

	#Check for 2 success = true and 1 success = false
	extract_xml $OUTPUT_DEST $XPATH_ENABLE_EVENT_SUCCESS result
	num_ocur=$(grep -o "true" <<< "$result" | wc -l)
	test "$num_ocur" -eq "2"
	ok $? "Mi test: Enabled event on failing enable command $num_ocur_true/2"

	extract_xml $OUTPUT_DEST $XPATH_ENABLE_EVENT_SUCCESS result
	num_ocur=$(grep -o "false" <<< "$result" | wc -l)
	test "$num_ocur" -eq "1"
	ok $? "Mi test: Enabled event on failing enable command $num_ocur_true/2"

	#Disable the event
	disable_ust_lttng_event $session_name ${event[0]} $channel_name
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "Mi test: Disable ust event xsd validation"
	is_command_success $OUTPUT_DEST false

	#Disable the 3 event combined. Should pass
	disable_ust_lttng_event $session_name $concat_event $channel_name
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "Mi test: Disable multiple ust event xsd validation"
	is_command_success $OUTPUT_DEST false

	#Make sure we have 3 inner success
	extract_xml $OUTPUT_DEST $XPATH_DISABLE_EVENT_SUCCESS result
	num_ocur=$(grep -o "true" <<< "$result" | wc -l)
	test "$num_ocur" -eq "3"
	ok $? "Mi test: Disable multiple ust event success $num_ocur/3"

	#Teardown
	OUTPUT_DEST=$DEVNULL
	destroy_lttng_sessions
}

function test_list_channel ()
{
	diag "Test list channel"

	local session_name="testSession"
	local channel_name="testChannel"
	local event=("ev1" "ev2" "ev3")

	OUTPUT_FILE="list_channel.xml"

	#Test buid up
	OUTPUT_DEST=$DEVNULL
	create_lttng_session_ok $session_name $OUTPUT_DIR
	enable_ust_lttng_channel_ok $session_name $channel_name

	#Enable ust and jul events
	concat_event=$( IFS=$','; echo "${event[*]}" )
	enable_ust_lttng_event_ok $session_name $concat_event $channel_name

	#Begin testing
	OUTPUT_DEST=$OUTPUT_DIR/$OUTPUT_FILE
	list_lttng_ok "$session_name -c $channel_name"
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "Mi test: list channel xsd validation"

	#Expect 1 domain: UST
	extract_xml $OUTPUT_DEST $XPATH_LIST_DOMAIN"/lttng:type/text()" result
	num=$(echo "$result" | wc -l)
	test "$num" -eq "1"
	ok $? "Mi test: $num / 1 domains discovered"

	#Expect 1 channel: test
	extract_xml $OUTPUT_DEST $XPATH_LIST_CHANNEL"/lttng:name/text()" result
	num=$(echo "$result" | wc -l)
	test "$num" -eq "1"
	ok $? "Mi test: $num / 1 channel discovered"

	test "$result" = "$channel_name"
	ok $? "Mi test: expected channel: $channel_name found: $result"

	#Add a channel and make sure we have 2 channel now
	OUTPUT_DEST=$DEVNULL
	enable_ust_lttng_channel_ok $session_name $channel_name"a"

	#Teardown
	OUTPUT_DEST=$DEVNULL
	destroy_lttng_sessions
}

function test_list_domain ()
{
	diag "Test list domain"

	local session_name="testSession"
	local channel_name="testChannel"
	local event=("ev1" "ev2" "ev3")

	OUTPUT_FILE="list_domain.xml"

	#Test buid up
	OUTPUT_DEST=$DEVNULL
	create_lttng_session_ok $session_name $OUTPUT_DIR
	enable_ust_lttng_channel_ok $session_name $channel_name

	#Enable ust and jul events
	concat_event=$( IFS=$','; echo "${event[*]}" )
	enable_ust_lttng_event_ok $session_name $concat_event $channel_name
	enable_jul_lttng_event $session_name $concat_event $channel_name

	#Begin testing
	OUTPUT_DEST=$OUTPUT_DIR/$OUTPUT_FILE
	list_lttng_ok "$session_name -d"
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "Mi test: list domain xsd validation"

	#Expect 2 domain: Jul and UST
	extract_xml $OUTPUT_DEST $XPATH_LIST_DOMAIN"/lttng:type/text()" result
	num=$(echo "$result" | wc -l)
	test "$num" -eq "2"
	ok $? "Mi test: $num / 2 domains discovered"

	#Test for valid value
	if [[ "$result" = *"JUL"* ]]; then
		pass "Mi test: domains Jul is present"
	else
		fail "Mi test: domains Jul is absent"
	fi

	if [[ "$result" = *"UST"* ]]; then
		pass "Mi test: domains UST is present"
	else
		fail "Mi test: domains UST is absent"
	fi

	#Teardown
	OUTPUT_DEST=$DEVNULL
	destroy_lttng_sessions
}

function test_list_session ()
{
	diag "Test list session"

	local session_name="testSession"
	local channel_name="testChannel"
	local event=("ev1" "ev2" "ev3")

	OUTPUT_FILE="list_session.xml"

	#Test buid up
	OUTPUT_DEST=$DEVNULL

	create_lttng_session_ok $session_name $OUTPUT_DIR
	enable_ust_lttng_channel_ok $session_name $channel_name

	#Enable ust and jul events
	concat_event=$( IFS=$','; echo "${event[*]}" )
	enable_ust_lttng_event_ok $session_name $concat_event $channel_name
	enable_jul_lttng_event $session_name $concat_event $channel_name

	#Begin testing
	OUTPUT_DEST=$OUTPUT_DIR/$OUTPUT_FILE
	list_lttng_ok $session_name
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "Mi test: list session xsd validation"

	#Expect 2 domain: Jul and UST
	extract_xml $OUTPUT_DEST $XPATH_LIST_DOMAIN"/lttng:type/text()" result
	num=$(echo "$result" | wc -l)
	test "$num" -eq "2"
	ok $? "Mi test: $num / 2 domains discovered"

	#Expect 2 channel: test and lttng_jul_event
	extract_xml $OUTPUT_DEST $XPATH_LIST_CHANNEL"/lttng:name/text()" result
	num=$(echo "$result" | wc -l)
	test "$num" -eq "2"
	ok $? "Mi test: $num / 2 channel discovered"

	#Teardown
	OUTPUT_DEST=$DEVNULL
	destroy_lttng_sessions
}

function test_list_ust_event ()
{
	diag "Test list ust event"

	local file_sync_after_first=$(mktemp -u -t "tmp.${FUNCNAME[0]}_sync_after_first.XXXXXX")
	local file_sync_before_last=$(mktemp -u -t "tmp.${FUNCNAME[0]}_sync_before_last.XXXXXX")

	OUTPUT_FILE="list_ust_event.xml"

	#Test buid up
	OUTPUT_DEST=$OUTPUT_DIR/$OUTPUT_FILE

	#Begin testing
	$TESTAPP_BIN -i $NR_ITER -w $NR_USEC_WAIT \
		--sync-after-first-event ${file_sync_after_first} \
		--sync-before-last-event ${file_sync_before_last} & 2>/dev/null

	while [ ! -f "${file_sync_after_first}" ]; do
		sleep 0.5
	done

	list_lttng_ok "-u -f"

	touch ${file_sync_before_last}

	$MI_VALIDATE $OUTPUT_DEST
	ok $? "Mi test: list ust event xsd validation"

	#Extract events
	extract_xml $OUTPUT_DEST "$XPATH_LIST_UST_EVENT""/lttng:name/text()" result
	num=$(echo "$result" | wc -l)

	#Since the number of events is prone to change we only look for >= from a
	#base number. An alternative would be to look for each events but this
	#is complicated for nothing.
	test "$num" -ge "5"
	ok $? "Mi test: $num / 5 ust events discovered"

	#Extract field from tp:tptest event. Expect >= to 12 because number of
	#field is prone to change.
	extract_xml $OUTPUT_DEST "$XPATH_LIST_UST_EVENT""[./lttng:name = 'tp:tptest']/lttng:event_fields/lttng:event_field/lttng:name/text()" result
	num=$(echo "$result" | wc -l)
	test "$num" -ge "12"
	ok $? "Mi test: $num / 12 ust event fields discovered"

	#Wait for all background processes
	wait

	rm -f ${file_sync_after_first}
	rm -f ${file_sync_before_last}
}

function test_start_stop ()
{
	diag "Test start-stop"

	local session_name="testStartStopSession"
	local channel_name="startStopChannel"

	OUTPUT_FILE="list_start_stop.xml"

	#Test buid up
	OUTPUT_DEST=$DEVNULL

	create_lttng_session_ok $session_name $OUTPUT_DIR

	#Test fail command
	#No channel enable start command should return element success false
	OUTPUT_DEST=$OUTPUT_DIR/$OUTPUT_FILE
	start_lttng_tracing_fail $session_name
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "Mi test: failed start xsd validation"

	#Expect a false element
	extract_xml $OUTPUT_DEST $XPATH_COMMAND_SUCCESS result
	test "$result" = "false"
	ok $? "Mi test: mi report failure on start command as expected"

	#Enable a channel to test success
	OUTPUT_DEST=$DEVNULL
	enable_ust_lttng_channel_ok $session_name $channel_name

	#Start tracing
	OUTPUT_DEST=$OUTPUT_DIR/$OUTPUT_FILE
	start_lttng_tracing_ok $session_name
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "Mi test: start xsd validation"

	#Expect a true element
	extract_xml $OUTPUT_DEST $XPATH_COMMAND_SUCCESS result
	test "$result" = "true"
	ok $? "Mi test: mi report success on start command as expected"

	#Extract session name from xml
	extract_xml $OUTPUT_DEST $XPATH_SESSION"/lttng:name/text()" result
	test "$result" = "$session_name"
	ok $? "Mi test: started $session_name"

	#Extract enabled expect true
	extract_xml $OUTPUT_DEST $XPATH_SESSION"/lttng:enabled/text()" result
	test "$result" = "true"
	ok $? "Mi test: enabled element : $result expected: true"

	#Test metadata regenerate command
	regenerate_metadata_ok $session_name
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "Mi test: regenerate metadata"

	#Test stop command
	stop_lttng_tracing_ok $session_name
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "Mi test: stop xsd validation"

	#Extract session name from xml
	extract_xml $OUTPUT_DEST $XPATH_SESSION"/lttng:name/text()" result
	test "$result" = "$session_name"
	ok $? "Mi test: stopped $session_name"

	#Extract enabled expect false
	extract_xml $OUTPUT_DEST $XPATH_SESSION"/lttng:enabled/text()" result
	test "$result" = "false"
	ok $? "Mi test: enabled element : $result expected: false"

	#Test failing stop command
	stop_lttng_tracing_ok $session_name
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "Mi test: stop warning xsd validation"

	#Expect a true element
	extract_xml $OUTPUT_DEST $XPATH_COMMAND_SUCCESS result
	test "$result" = "true"
	ok $? "Mi test: mi reports success on stop command as expected"

	#Teardown
	OUTPUT_DEST=$DEVNULL
	destroy_lttng_sessions
}

function test_snapshot ()
{
	diag "Test snapshot"

	local session_name="testSnapshotAdd"
	local snapshot_path="$OUTPUT_DIR/snapshotoutput"
	OUTPUT_FILE="snapshot.xml"

	#Test buid up
	OUTPUT_DEST=$DEVNULL
	create_lttng_session_no_output $session_name

	#Test fail command
	#No channel enable start command should return element success false
	OUTPUT_DEST=$OUTPUT_DIR/$OUTPUT_FILE
	lttng_snapshot_add_output_ok $session_name file://$snapshot_path
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "Mi test: snapshot xsd validation"

	#Expect a true element
	extract_xml $OUTPUT_DEST $XPATH_COMMAND_SUCCESS result
	test "$result" = "true"
	ok $? "Mi test: add snapshot is a success"

	#Get data
	extract_xml $OUTPUT_DEST "$XPATH_SNAPSHOT_ADD_SNAPSHOT""/lttng:snapshot/lttng:session_name/text()" result
	test "$result" = "$session_name"
	ok $? "Mi test: added snapshot for: $result expected $session_name"

	#Get name of generated output and id
	extract_xml $OUTPUT_DEST "$XPATH_SNAPSHOT_ADD_SNAPSHOT""/lttng:snapshot/lttng:name/text()" snapshot_name
	extract_xml $OUTPUT_DEST "$XPATH_SNAPSHOT_ADD_SNAPSHOT""/lttng:snapshot/lttng:id/text()" snapshot_id

	#Test list output
	lttng_snapshot_list $session_name
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "Mi test: snapshot list xsd validation"

	#Expect a true element
	extract_xml $OUTPUT_DEST $XPATH_COMMAND_SUCCESS result
	test "$result" = "true"
	ok $? "Mi test: add snapshot is a success"

	extract_xml $OUTPUT_DEST "$XPATH_SNAPSHOT_LIST""/lttng:session[./lttng:name ='$session_name']/lttng:snapshots/lttng:snapshot/lttng:name/text()" result
	test "$result" = "$snapshot_name"
	ok $? "Mi test: snapshot list extracted snapshot: $result expected: $snapshot_name"

	#Test del output
	lttng_snapshot_del_output_ok $session_name $snapshot_id
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "Mi test: snapshot delete xsd validation"

	#Success ?
	extract_xml $OUTPUT_DEST $XPATH_COMMAND_SUCCESS result
	test "$result" = "true"
	ok $? "Mi test: del snapshot is a success"

	#Test data
	extract_xml $OUTPUT_DEST "$XPATH_SNAPSHOT_DEL""/lttng:snapshot/lttng:id/text()" result
	test "$result" = "$snapshot_id"
	ok $? "Mi test: snapshot del extracted snapshot id: $result expected: $snapshot_id"

	extract_xml $OUTPUT_DEST "$XPATH_SNAPSHOT_DEL""/lttng:snapshot/lttng:session_name/text()" result
	test "$result" = "$session_name"
	ok $? "Mi test: snapshot del extracted snapshot id: $result expected: $session_name"

	#Test fail on delete command
	lttng_snapshot_del_output_fail $session_name $snapshot_id
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "Mi test: snapshot delete xsd validation"

	#Success ?
	extract_xml $OUTPUT_DEST $XPATH_COMMAND_SUCCESS result
	test "$result" = "false"
	ok $? "Mi test: del snapshot is a success"

	OUTPUT_DEST=$DEVNULL
	destroy_lttng_sessions
}

function test_track_untrack ()
{
	diag "Test track/untrack pid"

	local session_name="testTrack"
	local pid="1,2,3"
	OUTPUT_FILE="track.xml"

	#Test buid up
	OUTPUT_DEST=$DEVNULL
	create_lttng_session_no_output $session_name

	OUTPUT_DEST=$OUTPUT_DIR/$OUTPUT_FILE
	lttng_track_ok "-p $pid -u"
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "Mi test: track pid xsd validation"

	#Expect a true element
	extract_xml $OUTPUT_DEST $XPATH_COMMAND_SUCCESS result
	test "$result" = "true"
	ok $? "Mi test: track pid is a success"

	#Verify that there is 3 process
	extract_xml $OUTPUT_DEST $XPATH_TRACK_UNTRACK_VPID"/lttng:id" result
	num=$(echo "$result" | wc -l)
	test "$num" -eq "3"
	ok $? "Mi test: track pid expecting 3 processes got $num"

	#Test the pid_tracker listing
	list_lttng_ok $session_name
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "Mi test: session list with pid_tracker validate"

	#Check the good count
	extract_xml $OUTPUT_DEST $XPATH_VPID_TRACKER"/lttng:process_attr_values/lttng:vpid/lttng:type/lttng:id" result
	num=$(echo "$result" | wc -l)
	test "$num" -eq "3"
	ok $? "Mi test: tracker pid listing expecting 3 target got $num"

	#Untrack pid 2,3
	lttng_untrack_ok "-p 2,3 -u"
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "Mi test: untrack pid xsd validation"

	#Expect a true element
	extract_xml $OUTPUT_DEST $XPATH_COMMAND_SUCCESS result
	test "$result" = "true"
	ok $? "Mi test: untrack pid is a success"

	#Verify that there is 2 untracked pids
	extract_xml $OUTPUT_DEST $XPATH_TRACK_UNTRACK_VPID"/lttng:id" result
	num=$(echo "$result" | wc -l)
	test "$num" -eq "2"
	ok $? "Mi test: untrack pid expecting 2 process got $num"

	#Check pid_tracker listing
	list_lttng_ok $session_name
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "Mi test: session list with pid_tracker validate"

	#Check the good count
	extract_xml $OUTPUT_DEST $XPATH_VPID_TRACKER"/lttng:targets/lttng:vpid_target/lttng:type/lttng:pid" result
	num=$(echo "$result" | wc -l)
	test "$num" -eq "1"
	ok $? "Mi test: tracker pid listing expecting 1 process got $num"

	#Untrack all
	lttng_untrack_ok "-p -u -a"
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "Mi test: untrack pid xsd validation"

	#Expect a true element
	extract_xml $OUTPUT_DEST $XPATH_COMMAND_SUCCESS result
	test "$result" = "true"
	ok $? "Mi test: untrack all pid is a success"

	#Verify that there is 1 untracked pid
	extract_xml $OUTPUT_DEST $XPATH_TRACK_UNTRACK_VPID"/lttng:all" result
	num=$(echo "$result" | wc -l)
	test "$num" -eq "1"
	ok $? "Mi test: untrack pid expecting 1 process got $num"

	#Test if type "all" is "true" which represent the all argument
	test "$result" = "true"
	ok $? "Mi test: pid expected is true got $result"

	#Test if pid_tracker is enabled as definied by pid_tracker
	#behavior. If all pid are untrack than the pid_tracker is still
	#enabled (pid_tracker node is present).
	list_lttng_ok $session_name
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "Mi test: session list with pid_tracker validate"

	#Check the good count
	extract_xml $OUTPUT_DEST $XPATH_VPID_TRACKER"/lttng:targets/lttng:vpid_target/lttng:type/lttng:pid" result
	num=$(echo -n "$result" | wc -l)
	test "$num" -eq "0"
	ok $? "Mi test: tracker pid listing expecting 0 process got $num"

	#Test track all
	lttng_track_ok "-p -u -a"
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "Mi test: track pid xsd validation"

	#Expect a true element
	extract_xml $OUTPUT_DEST $XPATH_COMMAND_SUCCESS result
	test "$result" = "true"
	ok $? "Mi test: track all pid is a success"

	#Verify that there is 1 tracked process
	extract_xml $OUTPUT_DEST $XPATH_TRACK_UNTRACK_VPID"/lttng:all" result
	num=$(echo "$result" | wc -l)
	test "$num" -eq "1"
	ok $? "Mi test: track pid expecting 1 process got $num"

	#Test if the id is true wich represent the all argument
	test "$result" = "true"
	ok $? "Mi test: pid expected is true got $result"

	#Test if pid_tracker is enabled as definied by pid_tracker
	#behavior. If all pid are untrack thant the pid_tracker is
	#disabled (node pid_tracker do not exist)
	list_lttng_ok $session_name
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "Mi test: session list with pid_tracker validate"

	node_check_xml $OUTPUT_DEST $XPATH_VPID_TRACKER result
	test "$result" = ""
	ok $? "Mi test: VPID_tracker node is absent as defined"

	OUTPUT_DEST=$DEVNULL
	destroy_lttng_sessions
}

function test_add_context_list()
{
	diag "Test context listing"

	OUTPUT_FILE="list-context.xml"
	OUTPUT_DEST=$OUTPUT_DIR/$OUTPUT_FILE
	lttng_add_context_list

	$MI_VALIDATE $OUTPUT_DEST
	ok $? "Mi test: context listing validation"
}

function test_clear_session ()
{
	diag "Test clear session"

	# Since the session are not started there is no real clear done.
	# We are testing the MI output only here.
	local session_name=(
			"testSession1"
			"testSession2"
			"testSession3")

	OUTPUT_FILE="clear_session.xml"

	#Test build up
	OUTPUT_DEST=$DEVNULL
	for (( i = 0; i < 3; i++ )); do
		create_lttng_session_ok ${session_name[$i]} $OUTPUT_DIR
	done

	OUTPUT_DEST=$OUTPUT_DIR/$OUTPUT_FILE
	lttng_clear_session_ok  ${session_name[0]}
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "MI test: clear session ${session_name[0]} xsd validation"

	#Verify that we cleared the good session
	extract_xml $OUTPUT_DEST $XPATH_SESSION"/lttng:name/text()" result
	test "$result" = "${session_name[0]}"
	ok $? "Mi test: cleared session: $result expected: ${session_name[0]}"

	#Destroy all and count:should be 3
	lttng_clear_all
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "MI test: clear all session xsd validation"

	#Verify that we cleared 3 sessions
	extract_xml $OUTPUT_DEST $XPATH_SESSION"/lttng:name/text()" result
	num=$(echo "$result" | wc -l)
	test "$num" -eq "3"
	ok $? "Mi test: $num / 3 sessions discovered"

	destroy_lttng_sessions

	#Verify that if no session is present command fail and mi report it.
	lttng_clear_session_fail ${session_name[0]}
	$MI_VALIDATE $OUTPUT_DEST
	ok $? "MI test: failing clear session ${session_name[0]} xsd validation"
	extract_xml $OUTPUT_DEST $XPATH_COMMAND_SUCCESS result
	test "$result" = "false"
	ok $? "Mi test: clear command on ${session_name[0]} failed as expected"

}

start_lttng_sessiond $SESSIOND_LOAD_DIR
TESTS=(
	test_version
	test_create_session
	test_destroy_session
	test_list_sessions
	test_ust_channel
	test_ust_lttng_event
	test_list_channel
	test_list_domain
	test_list_session
	test_list_ust_event
	test_start_stop
	test_snapshot
	test_track_untrack
	test_list_session_long_path
	test_add_context_list
	test_clear_session
)


echo $OUTPUT_DIR
for fct_test in ${TESTS[@]};
do
	${fct_test}
	if [ $? -ne 0 ]; then
		break;
	fi
done
rm -rf $OUTPUT_DIR
rm -rf $SESSIOND_LOAD_DIR

OUTPUT_DEST=/dev/null 2>&1
stop_lttng_sessiond
